
menu();
function menu(){
	$.post("menu.do",function(data){
		var html="";
		for (var i = 0; i < data.data.length; i++) {
			html+='<input type="checkbox" name="checkbox1" title='+data.data[i].mNames+' value='+data.data[i].mId+'>';
		}
		$("#menusub").html(html);
		sub();
	},"json")
}

function sub(){
	$.post("sub.do",function(data){
		var html="";
		for (var i = 0; i < data.data.length; i++) {
			html+=" <input type='checkbox' name='checkbox2' title="+data.data[i].sPathName+" value="+data.data[i].sId+">";
		}
		$("#sub").html(html);
		rederAuth()
	},"json")
}

function rederAuth() {
	var id = $("#pId").val();
	$.post("viewPermissionfindMen.do?id=" + id, function(data) {
		for (var i = 0; i < data.data.length; i++) {
			$("input[name=checkbox1][value="+data.data[i].mId+"]").prop("checked", true);
		}
		$.post("viewPermissionfindSub.do?id=" + id, function(data) {
			for (var i = 0; i < data.data.length; i++) {
				$("input[name=checkbox2][value="+data.data[i].sId+"]").prop("checked", true);
			}
			layui.form.render();
		}, "json")
	}, "json")
}

function modifyAuth() {
	var id = $("#pId").val();
	var arr = new Array();
	var arr2 = new Array();
	$("input:checkbox[name='checkbox1']:checked").each(function(i) {
		arr[i] = $(this).val();
	});
	$("input:checkbox[name='checkbox2']:checked").each(function(i) {
		arr2[i] = $(this).val();
	});
	$.post("modifyAuthority.do", {
		pMainId : arr.join(","),
		pSubId : arr2.join(","),
		id : id
	}, function(res) {
		if (res.type == 1) {
			var infoindex = top.layer.alert("修改成功", {
				icon : 6
			},
			function() {
				// 获得frame索引
				var index = parent.layer
						.getFrameIndex(window.name);
				//关闭当前frame
				parent.layer.close(index);
				top.layer.close(infoindex);
				window.location.reload();
			});
		} else {
			window.location.reload();
			layer.msg(data.msg, {
				area : [ '300px', '200px' ]
			});
		}
	}, "json")
	return false;
}